<?php

//laravel中的Gate, 是用来决定用户是否授权访问给定的动作的闭包函数，App\Providers\AuthServiceProvider

/*
  注册:
   在App\Providers\AuthServiceProvider;
        public function boot()
        {
            $this->registerPolicies();
            门卫
            Gate::define('update-post', function ($user, $post) {
                return $user->id == $post->user_id;
            });
        }
  使用:
     //route中
     Route::group(['middleware'=>'can:system'],function(){
         //需要守卫的路由，也就是模块的路由
     });

     //控制器中
     $this->authorize('update',$post);
     

     //在视图中
     @can('system');

 

*/


//应用
        //1.在App\Providers\AuthServiceProvider中
        //取出所有的权限-----对应模块
        $permissions = \App\AdminPermission::with('roles')->get();
        //对所有的权限都要进行注册，有几个权限就注册几个Gate
        foreach ($permissions as $permission) {
            //门卫的名字就用权限的名称
            Gate::define($permission->name, function($user) use($permission) {
                return $user->hasPermission($permission);
            });
        }

        //2.路由中的使用

        Route::group(['middleware' => 'can:system'], function(){
            // 用户管理
            Route::get('/users', '\App\Admin\Controllers\UserController@index');
            Route::get('/users/create', '\App\Admin\Controllers\UserController@create');
            Route::post('/users/store', '\App\Admin\Controllers\UserController@store');
            Route::get('/users/{user}/role', '\App\Admin\Controllers\UserController@role');
            Route::post('/users/{user}/role', '\App\Admin\Controllers\UserController@storeRole');

            // 角色管理
            Route::get('/roles', '\App\Admin\Controllers\RoleController@index');
            Route::get('/roles/create', '\App\Admin\Controllers\RoleController@create');
            Route::post('/roles/store', '\App\Admin\Controllers\RoleController@store');
            Route::get('/roles/{role}/permission', '\App\Admin\Controllers\RoleController@permission');
            Route::post('/roles/{role}/permission', '\App\Admin\Controllers\RoleController@storePermission');

            // 权限管理
            Route::get('/permissions', '\App\Admin\Controllers\PermissionController@index');
            Route::get('/permissions/create', '\App\Admin\Controllers\PermissionController@create');
            Route::post('/permissions/store', '\App\Admin\Controllers\PermissionController@store');
        });

        // 文章管理
        Route::group(['middleware' => 'can:post'], function() {
            // 文章管理
            Route::get('/posts', '\App\Admin\Controllers\PostController@index');
            Route::post('/posts/{post}/status', '\App\Admin\Controllers\PostController@status');
        });
        //3.修改模板

          @can('system')
            <li class="treeview active">
                <a href="#">
                    <i class="fa fa-dashboard"></i> <span>系统管理</span>
                    <span class="pull-right-container"></span>
                </a>
                <ul class="treeview-menu">
                    <li><a href="/admin/permissions"><i class="fa fa-circle-o"></i> 权限管理</a></li>
                    <li><a href="/admin/users"><i class="fa fa-circle-o"></i> 用户管理</a></li>
                    <li><a href="/admin/roles"><i class="fa fa-circle-o"></i> 角色管理</a></li>
                </ul>
            </li>
            @endcan
            @can('post')
            <li class="active treeview">
                <a href="/admin/posts">
                    <i class="fa fa-dashboard"></i> <span>文章管理</span>
                </a>
            </li>
            @endcan